﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace INF1010_Chat
{
    public class UserList
    {
        private List<User> users;

        public UserList()
        {
            users = new List<User>();
        }

        public List<User> Users
        {
            get
            {
                return users.ToList();
            }
        }

        public void AddUser(User user)
        {
            if (!users.Contains(user))
                users.Add(user);
        }

        public void RemoveUser(User user)
        {
            if (users.Contains(user))
                users.Remove(user);
        }

        public string Serialize()
        {
            string result = String.Empty;
            if(users.Any())
                result = String.Join("%?&", users.Where(u => u.IsAuthenticated).Select(u => u.Serialize()));
            return result;
        }

        public static UserList Deserialize(String source)
        {
            List<string> usersName = source.Split("%?&".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            UserList userList = new UserList();
            foreach (string userName in usersName)
            {
                User user = User.Deserialize(userName);
                userList.AddUser(user);
            }
            return userList;
        }
    }
}
